#!/usr/bin/env python3
# This script takes a location as a command-line argument, and queries
# wttr.in for the weather at the location, graphing the average temperatures
# for the next three days 
import sys
import numpy as np
import matplotlib.pyplot as plt
import urllib.request
import json

def request_json(url):
    with urllib.request.urlopen(url) as response:
        data = response.read()
    return data

def main():
    location = sys.argv[1]   
    data = request_json('https://wttr.in/' + location + '?format=j1')
    jsonobj = json.loads(data.decode("utf-8"))
    temperatures = []
    dates = []

    for record in jsonobj['weather']:
        dates.append(record['date'])
        # wttr.in temp data is treated as a string
        # by the json module so a conversion is needed
        data_point = int(record['avgtempF'])
        temperatures.append(data_point)
    # size = len(temperatures)
    # sorted_temps = temperatures.copy()
    # sorted_temps.sort()
    # plt.plot(temperatures)
    # plt.plot([0, 1, 2], temperatures)
    plt.plot(dates, temperatures, 'b-o')
    plt.axis([-1, 3, 60, 75])
    plt.ylabel('Avg. Temp F')
    plt.xlabel('Date')
    plt.show()
main()
